<?php
/**
 * --------------------------------------------------------------------
 * 角色
 * --------------------------------------------------------------------
 * @author Midnight, yangyunzhou@foxmail.com
 * --------------------------------------------------------------------
 * @copyright Midnight Studio
 * --------------------------------------------------------------------
 */
class RoleAction extends AdminBaseAction {

	/**
	 * --------------------------------------------
	 * 角色管理
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	public function manage() {
		$this->title = '角色管理';
		$this->list = $this->get_list();
		$this->display('Admin:role/manage');
	}

	/**
	 * --------------------------------------------
	 * 添加角色
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	public function add() {
		if($this->isFormPost() && $this->isValidToken()) {
			$db = D('Role');
			if(!$db->create()) $this->error($db->getError());
			if($db->add()) $this->success('添加组 "' . $_POST['name'] . '" 成功');
			else $this->error('添加组 "' . $_POST['name'] . '" 失败');
		} else {
			$this->title = '添加角色';
			$this->display('Admin:role/add');
		}
	}

	/**
	 * --------------------------------------------
	 * 修改角色
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 */
	public function edit() {
		if($this->isFormPost() && $this->isValidToken()) {
			$db = D('Role');
			if(!$db->create()) $this->error($db->getError());
			$db->{$db->pk} = $_GET['id'];
			if($db->save()) $this->success('修改成功！');
			else $this->success('没有任何改变');
		} else {
			$this->title = '角色编辑';
			$db = D('Role');
			$val = $db->find($_GET['id']);
			$this->assign('val',$val);
			$this->getRadio(array(1=>'启用',0=>'禁用'),'state',$val['state']);
			$this->display('Admin:role/edit');
		}
	}

	/**
	 * --------------------------------------------
	 * 获取角色列表
	 * --------------------------------------------
	 * @author  Midnight,  yangyunzhou@foxmail.com
	 */
	private function get_list() {
		Import('org.util.page');
		$db = D('Role');
		$page = new Page($db->count(),$this->pageCount);
		$val = $db->field('id,name,state')->order('`id` DESC')->select();
		$this->page = $page->show();
		return $val;
	}

	/**
	 * --------------------------------------------
	 * 项目授权
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 */
	public function accredit() {
		if($this->isFormPost() && $this->isValidToken()) {
			$Access = D('Access');
			$role  = $_POST['role'];
			$level = $_POST['level'];
			$node  = $_POST['node'];
			$_node = $_POST['hidden_node'];

			// 对比授权, 删除异常授权
			if( !empty($_node) && !empty($node) ) {
				$difference = array_diff($_node, $node);
				if(!empty($difference)) $Access->del($role, $difference, $level);
			}
			// 删除节点授权
			elseif( !empty($_node) && empty($node) ) {
				$Access->del($role, $_node, $level);
			}

			// 添加节点授权
			foreach( $node as $v ) {
				$Access->set($role, $v, $level);
			}

			// 操作完成
			$this->success('操作完成');
		} else {
			$this->title = '角色授权';

			/* 授权类型 */
			$type = array(
				0=>array('id'=>'-1', 'name'=>'选择授权类型'),
				1=>array('id'=>'app', 'name'=>'应用授权'),
				2=>array('id'=>'model', 'name'=>'模块授权'),
				3=>array('id'=>'action', 'name'=>'操作授权')
			);
			$this->getSelect($type, array('id','name'), $_GET['type'], 'type', TRUE);

			/* 用户组 (无论无何输出) */
			$node = D('Node');
			$role = D('Role');
			$_role = $role->get_role_list();
			if($role) {
				$_role = array('-1'=>array('id'=>'-1', 'name'=>'选择角色')) + $_role;
				$this->getSelect($_role, array('id', 'name', 'title'), $_GET['role'], 'role', TRUE);
			}

			/*
			 * 应用
			 *
			 *  必选: 用户组
			 */
			if(($_GET['type'] == 'model' || $_GET['type'] == 'action') && $_GET['role']) {
				$app = $node->children(1);
				if($app) {
					$app = array('-1'=>array('id'=>'-1','name'=>'选择应用')) + $app;
					$this->getSelect($app, array('id','name','title'), $_GET['app'], 'app', TRUE);
				}
			}

			/*
			 * 模块
			 *
			 *  必选: 用户组、应用
			 */
			if($_GET['type'] == 'action' && $_GET['app']) {
				$model = $node->children(2);
				if($model) {
					$model = array('-1'=>array('id'=>'-1','name'=>'选择模块')) + $model;
					$this->getSelect($model, array('id','name','title'), $_GET['model'], 'model', TRUE);
				}
			}

			/* 应用 */
			if($_GET['type'] == 'app' && $_GET['role'] == TRUE) {
				$this->assign('level', 1);
				$re = $node->children(1);
				$access = D('Access')->get($_GET['role'],1);
                $this->getCheckbox($re, array(0=>'id','value'=>'id', 'names'=>'node[]', 'name'=>'name'), 'list', $access);
			}
			/* 模块 */
			elseif($_GET['type'] == 'model' && $_GET['app']) {
				$this->assign('level', 2);
				$re = $node->children(2, $_GET['app']);
				$access = D('Access')->get($_GET['role'],2);
				$this->getCheckbox($re, array(0=>'id','value'=>'id','names'=>'node[]','name'=>'name'), 'list', $access);
			}
			/* 操作 */
			elseif($_GET['type'] == 'action') {
				$this->assign('level', 3);
				$re = $node->children(3,$_GET['model']);
				$access = D('Access')->get($_GET['role'],3);
				$this->getCheckbox($re, array(0=>'id','value'=>'id','names'=>'node[]','name'=>'name'), 'list', $access);
			}
			$this->display('Admin:role/accredit');
		}

	}

}